package yibao.youyun.com.yibao.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.zanlabs.widget.infiniteviewpager.InfiniteViewPager;
import com.zanlabs.widget.infiniteviewpager.indicator.CirclePageIndicator;

import yibao.youyun.com.yibao.Adapter.LargeGalleryAdapter;
import yibao.youyun.com.yibao.Adapter.SmallGalleryAdapter;
import yibao.youyun.com.yibao.Adapter.ListViewAdapter;
import yibao.youyun.com.yibao.Adapter.MockPagerAdapter;
import yibao.youyun.com.yibao.R;

/**
 * Created by woodnaonly on 2015/11/20.
 */
public class HomeFragment extends Fragment
{


    public HomeFragment()
    {
        super();
    }
    private RecyclerView mRecyclerView4;
    private RecyclerView mRecyclerView3;
    private RecyclerView mRecyclerView2;
    private SmallGalleryAdapter mAdapter;
    private InfiniteViewPager mInfiniteViewpagr;
    private View view;
    private CirclePageIndicator indicator;
    private int []mImageViewId={R.drawable.a,
            R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e,
            R.drawable.f, R.drawable.g, R.drawable.h, };
    private RecyclerView mRecyclerView;
    private ListView mListview;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.home_layout, null, false);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (mInfiniteViewpagr != null)
            mInfiniteViewpagr.startAutoScroll();
    }

    @Override
    public void onStop()
    {
        if (mInfiniteViewpagr != null)
            mInfiniteViewpagr.stopAutoScroll();
        super.onStop();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        this.view = view;
        initviewtoppager();
        initrecyclerview();

    }

    private void initrecyclerview()
    {
        mRecyclerView = (RecyclerView)view.findViewById(R.id.id_recyclerview_horizontal);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new SmallGalleryAdapter(getContext(), mImageViewId);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickLitener(new SmallGalleryAdapter.OnItemClickLitener()
        {
            @Override
            public void onItemClick(View view, int position)
            {
                Snackbar.make(view, position + "", Snackbar.LENGTH_LONG).show();
            }
        });


        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getContext());
        linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView2 = (RecyclerView)view.findViewById(R.id.id_recyclerview_horizonta2);
        mRecyclerView2.setLayoutManager(linearLayoutManager2);
        mRecyclerView2.setAdapter(mAdapter);


        LargeGalleryAdapter mAdapter3 = new LargeGalleryAdapter(getContext(), mImageViewId);
        LinearLayoutManager linearLayoutManager3 = new LinearLayoutManager(getContext());
        linearLayoutManager3.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView3 = (RecyclerView)view.findViewById(R.id.id_recyclerview_horizonta3);
        mRecyclerView3.setLayoutManager(linearLayoutManager3);
        mRecyclerView3.setAdapter(mAdapter3);

//        mRecyclerView4=(RecyclerView)view.findViewById(R.id.id_recyclerview_vertical);
//        VerticalAdapter mVerticalAdapter = new VerticalAdapter(getContext(), mImageViewId);
//        LinearLayoutManager linearLayoutManager4 = new FullyLinearLayoutManager(getContext());
//        linearLayoutManager4.setOrientation(LinearLayoutManager.VERTICAL);
//        mRecyclerView4.setLayoutManager(linearLayoutManager4);
//        mRecyclerView4.setAdapter(mVerticalAdapter);
//        mRecyclerView4.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL_LIST));
        mListview= (ListView) view.findViewById(R.id.home_layout_listview);
        mListview.setAdapter(new ListViewAdapter(getContext(),mImageViewId));

    }

    private void initviewtoppager()
    {
        mInfiniteViewpagr = (InfiniteViewPager) view.findViewById(R.id.topviewpager);
        indicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        MockPagerAdapter pagerAdapter = new MockPagerAdapter(getContext());
        mInfiniteViewpagr.setAdapter(pagerAdapter);
        mInfiniteViewpagr.setAutoScrollTime(2000);
        mInfiniteViewpagr.startAutoScroll();
        indicator.setViewPager(mInfiniteViewpagr);
        indicator.setCentered(false);


    }



}
